package com.exam.controller;

import com.exam.pojo.*;
import com.exam.service.FillQuestionService;
import com.exam.service.JudgeQuestionService;
import com.exam.service.MultiQuestionService;
import com.exam.service.PaperService;
import com.exam.util.ApiResultHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class PaperController {
    @Autowired
    private PaperService paperService;
    @Autowired
    private JudgeQuestionService judgeQuestionService;
    @Autowired
    private MultiQuestionService multiQuestionService;
    @Autowired
    private FillQuestionService fillQuestionService;
    @GetMapping("/papers")
    public ApiResult<PaperManage> findAll(){
        ApiResult res=ApiResultHandler.buildApiResult(200, "请求成功",paperService.findAll());
        return res;
    }
    @GetMapping("/paper/{paperId}")
    public Map<Integer, List<?>> findById(@PathVariable("paperId") Integer paperId){
        List<MultiQuestion> multiQuestionsRes=multiQuestionService.findByIdAndType(paperId);
        List<FillQuestion> FillQuestionRes=fillQuestionService.findByIdAndType(paperId);
        List<JudgeQuestion> judgeQuestionRes=judgeQuestionService.findByIdAndType(paperId);
        Map<Integer, List<?>> map = new HashMap<>();
        map.put(1, multiQuestionsRes);
        map.put(2, FillQuestionRes);
        map.put(3, judgeQuestionRes);
        return map;
    }
    @PostMapping("/paperManage")
    public ApiResult add(@RequestBody PaperManage paperManage) {
        int res = paperService.add(paperManage);
        if (res != 0) {
            return ApiResultHandler.buildApiResult(200,"添加成功",res);
        }
        return ApiResultHandler.buildApiResult(400,"添加失败",res);
    }

}
